package com.reader.cms.service.impl.topic;

import com.baomidou.mybatisplus.plugins.Page;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.reader.cms.entity.common.ReaderContentClassifyRelation;
import com.reader.cms.entity.common.ReaderContentTagsRelation;
import com.reader.cms.entity.magazine.ReaderMagazineContent;
import com.reader.cms.entity.topic.ReaderTopic;
import com.reader.cms.mapper.common.ReaderContentClassifyRelationMapper;
import com.reader.cms.mapper.common.ReaderContentTagsRelationMapper;
import com.reader.cms.mapper.topic.ReaderTopicMapper;
import com.reader.cms.service.topic.IReaderTopicService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author gumenghao
 * @@description 服务实现类
 * @date 2018-11-06
 */
@Service
@Transactional
public class ReaderTopicServiceImpl extends ServiceImpl<ReaderTopicMapper, ReaderTopic> implements IReaderTopicService {

    @Autowired
    private ReaderTopicMapper readerTopicMapper;

    @Autowired
    private ReaderContentTagsRelationMapper readerContentTagsRelationMapper;

    @Autowired
    private ReaderContentClassifyRelationMapper readerContentClassifyRelationMapper;

    /**
     * @param currentPage 当前页
     * @param size        每页显示条数
     * @param title       其它参数，自行定义
     * @return
     * @function 多表关联查询
     */
    @Transactional(readOnly = true)
    @Override
    public Page<ReaderTopic> selectListPage(int currentPage, int size, String title) {
        Page<ReaderTopic> page = new Page<>(currentPage, size);// 当前页，总条数 构造 page 对象
        return page.setRecords(readerTopicMapper.selectListPage(page, title));
    }

    @Override
    public boolean insertTopic(ReaderTopic readerTopic) {
        boolean resultBoolean = this.insert(readerTopic);
        if(resultBoolean) {
            //添加专题和标签关联表
            if(readerTopic.getTagIds().size() != 0){
                insertTopicTags(readerTopic);
            }
            //添加专题和内容分类关联表
            if(readerTopic.getContentClassifies().size()!= 0){
                insertTopicClassifies(readerTopic);
            }
        }
        return resultBoolean;
    }

    @Override
    public boolean updateTopic(ReaderTopic readerTopic) {
        return false;
    }


    //添加专题和分类关联表
    private void insertTopicClassifies(ReaderTopic readerTopic) {
        List<ReaderContentClassifyRelation> relations = new ArrayList<>();
        List<Integer> contentClassifies = readerTopic.getContentClassifies();
        contentClassifies.forEach((contentClassifyId) -> {
            ReaderContentClassifyRelation relation = new ReaderContentClassifyRelation();
            relation.setClassifyId(contentClassifyId);
            relation.setDocId(readerTopic.getId());
            relation.setInsertDt(new Date());
            relation.setInsertUserId(readerTopic.getInsertUserId());
            relation.setUpdtUserId(readerTopic.getUpdtUserId());
            relations.add(relation);
        });
        readerContentClassifyRelationMapper.insertBaths(relations);

    }


    //添加专题和标签关联表
    private void insertTopicTags(ReaderTopic readerTopic) {
        List<ReaderContentTagsRelation> relations = new ArrayList<>();
        List<Integer> tagIds = readerTopic.getTagIds();
        tagIds.forEach((tagId) -> {
            ReaderContentTagsRelation relation = new ReaderContentTagsRelation();
            relation.setDocId(readerTopic.getId());
            relation.setTagId(tagId);
            relation.setInsertDt(new Date());
            relation.setInsertUserId(readerTopic.getInsertUserId());
            relation.setUpdtUserId(readerTopic.getUpdtUserId());
            relations.add(relation);
        });
        readerContentTagsRelationMapper.insertBaths(relations);

    }

}